﻿using KBWebApi.Common;
using KBWebApi.Model;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace KBWebApi.Dal
{
    public class WorkShopDal
    {
        private static SqlSugarScope db = SqlDBHelper.SqlHelper.GetDB();

        public static bool IsExists(WorkShopModel workShop)
        {
            try
            {
                bool isexists = db.Queryable<WorkShopModel>().Any(t => t.XS_WorkShopId == workShop.XS_WorkShopId);
                return isexists;
            }
            catch (Exception ex)
            {
                WriteLog.Writelog("WorkStation.IsExists=>" + ex.ToString());
                return false;
            }
        }

        public int NewKBInput(WorkShopModel workShop)
        {
            int count = db.Insertable(workShop).ExecuteCommand();
            return count;
        }

        public int Update(WorkShopModel workShop)
        {
            int count = db.Updateable(workShop).IgnoreColumns(t => new { t.CreateDate }).ExecuteCommand();
            return count;
        }

        public int Delete(string WorkShopId)
        {
            int count = db.Deleteable<WorkShopModel>().Where(t => t.XS_WorkShopId == int.Parse(WorkShopId)).ExecuteCommand();
            return count;
        }

        public static DataTable GetAllList()
        {
            return db.Queryable<WorkShopModel>()
                .With(SqlWith.NoLock)
                .OrderBy(t => new { t.XS_WorkShopId, t.CreateDate })
                .ToDataTable();
        }
    }
}
